home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 January / macpower199701.bin / AMUG / Programming_10 / WASTE 1.3a1.sit / WASTE 1.3a1 Distribution / WASTE 1.3a1 / WEDebug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-23  |  2.5 KB  |  93 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    WEDebug.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *  Debugging Routines
  6.  *
  7.  *  Copyright (c) 1993-1996 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  *  C port by Dan Crevier
  11.  *
  12.  */
  13.  
  14.  
  15. #include "WASTEIntf.h"
  16.  
  17. #if WASTE_DEBUG
  18.  
  19. pascal void _WESanityCheck(WEHandle hWE)
  20. {
  21.     // _WESanityCheck performs several checks on two key data structures:
  22.     // the run array and the style table, verifying a number of assertions.
  23.     // This routine made it possible to identify many subtle bugs during development.
  24.  
  25.     WEPtr pWE = *hWE;                // we aren't going to move memory
  26.     SInt32 runIndex;
  27.     SInt32 styleIndex;
  28.     RunArrayElementPtr pRun;
  29.     StyleTableElementPtr pStyle;
  30.  
  31.     // check the consistency of the run array
  32.     pRun = *pWE->hRuns;
  33.  
  34.     // first runStart must be zero
  35.     WEASSERT(pRun->runStart == 0, "¥pFirst run array element is bad");
  36.  
  37.     for ( runIndex = 0; runIndex < pWE->nRuns; runIndex++ )
  38.     {
  39.         // all runs must be at least one character long
  40.         WEASSERT((pRun[1].runStart - pRun[0].runStart) > 0, "¥pRun length less than one");
  41.  
  42.         // no two consecutive runs may reference the same style
  43.         WEASSERT(pRun[1].styleIndex != pRun[0].styleIndex, "¥pSpurious run boundary");
  44.  
  45.         // all run array elements (except the last dummy entry) must reference an existing style
  46.         WEASSERT((pRun->styleIndex >= 0) && (pRun->styleIndex < pWE->nStyles), "¥pInvalid style index");
  47.         pRun++;
  48.     }
  49.  
  50.     // last (dummy) runStart must be textLength + 1 and styleIndex must be -1
  51.     WEASSERT((pRun->runStart == pWE->textLength + 1) && (pRun->styleIndex == -1),
  52.                 "¥pLast run array element is bad");
  53.  
  54.     pStyle = *pWE->hStyles;
  55.     for ( styleIndex = 0; styleIndex < pWE->nStyles; styleIndex++ )
  56.     {
  57.         SInt32 refCount;
  58.  
  59.         // the number of runs referencing each style in the style table
  60.         // must match the style reference count
  61.         refCount = 0;
  62.         pRun = *pWE->hRuns;
  63.         for ( runIndex = 0; runIndex < pWE->nRuns; runIndex++ )
  64.         {
  65.             if (pRun->styleIndex == styleIndex)
  66.                 refCount++;
  67.             pRun++;
  68.         }
  69.         WEASSERT(pStyle->refCount == refCount, "¥pBad style reference count");
  70.  
  71.         // there may not be two identical entries in the style table (except for unused entries)
  72.         if (pStyle->refCount > 0)
  73.         {
  74.             StyleTableElementPtr pStyle2;
  75.             SInt32 styleIndex2;
  76.  
  77.             pStyle2 = pStyle;
  78.             for ( styleIndex2 = styleIndex + 1; styleIndex2 < pWE->nStyles - 1; styleIndex2++ )
  79.             {
  80.                 pStyle2++;
  81.                 if (pStyle2->refCount > 0)
  82.                 {
  83.                     WEASSERT(!_WEBlockCmp(&pStyle->info, &pStyle2->info, sizeof(WERunAttributes)),
  84.                             "¥pDuplicate entry in style table");
  85.                 }
  86.             }
  87.         }
  88.         pStyle++;
  89.     }
  90. }
  91.  
  92. #endif  // WASTE_DEBUG
  93.